﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using lumo.display.object2D;

namespace lumo.display.animation2D
{
    /// <summary>
    /// Scale animation.
    /// </summary>
    public class CropAnimation2D : GenericAnimation2D<ICroppable>
    {
        // Private variables
        private Rectangle? From = null;
        private Rectangle To;
        private Counter XCounter = new Counter();
        private Counter YCounter = new Counter();
        private Counter WidthCounter = new Counter();
        private Counter HeightCounter = new Counter();

        /// <summary>
        /// Constructor
        /// </summary>
        public CropAnimation2D(Rectangle To, float Time, Smooth Smoothness = Smooth.None, Loop LoopMode = Loop.None, ICroppable CurrentObject = null)
            : base(Time, Smoothness, LoopMode, CurrentObject)
        {
            this.To = To;
        }

        /// <summary>
        /// Constructor
        /// </summary>
        public CropAnimation2D(Rectangle? From, Rectangle To, float Time, Smooth Smoothness = Smooth.None, Loop LoopMode = Loop.None, ICroppable CurrentObject = null)
            : base(Time, Smoothness, LoopMode, CurrentObject)
        {
            this.From = From;
            this.To = To;
        }

        /// <summary>
        /// Check if finished
        /// </summary>
        public override bool Finished { get { return (XCounter.Finished && YCounter.Finished && WidthCounter.Finished && HeightCounter.Finished); } }

        /// <summary>
        /// Setup the values
        /// </summary>
        internal override void SetupValues()
        {
            if (!this.From.HasValue)
                this.From = this.CurrentObject.SourceRectangle;
            XCounter.Reset(this.From.Value.X, this.To.X, this.Time, this.Smoothness, this.LoopMode);
            YCounter.Reset(this.From.Value.Y, this.To.Y, this.Time, this.Smoothness, this.LoopMode);
            WidthCounter.Reset(this.From.Value.Width, this.To.Width, this.Time, this.Smoothness, this.LoopMode);
            HeightCounter.Reset(this.From.Value.Height, this.To.Height, this.Time, this.Smoothness, this.LoopMode);
        }

        /// <summary>
        /// Update the values.
        /// </summary>
        /// <param name="Game">The game class</param>
        internal override void UpdateValues(LumoComponent Game)
        {
            XCounter.Update(Game);
            YCounter.Update(Game);
            WidthCounter.Update(Game);
            HeightCounter.Update(Game);
            this.CurrentObject.SourceRectangle = new Rectangle((int)XCounter.Value, (int)YCounter.Value, (int)WidthCounter.Value, (int)HeightCounter.Value);
        }
    }
}
